package com.backstage.productModule.controller;


import com.backstage.lyorder.pojo.QueryData;
import com.backstage.productModule.pojo.Commodity;
import com.backstage.productModule.pojo.Details;
import com.backstage.productModule.pojo.ProductList;
import com.backstage.productModule.service.CommodityService;
import com.backstage.productModule.service.DetailsService;
import com.backstage.productModule.service.ProductListService;
import com.backstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author sakura
 * @since 2022-12-14
 */
@RestController
@RequestMapping("productList")
@CrossOrigin
public class ProductListController {
    @Autowired
    private ProductListService productListService;

    @Autowired
    private CommodityService commodityService;

    @Autowired
    private DetailsService detailsService;


    /**
     * 模糊分页查询
     * @param queryData
     * @return
     */
    @PostMapping("list")
    public R getList(@RequestBody QueryData queryData){
        Page<ProductList> page = new Page<>(queryData.getCp(), queryData.getCp());
        productListService.page(page);
        //查询商品一级分类
        List<ProductList> list = page.getRecords();
//        List<ProductList> list = productListService.list();
        //遍历一级分类 将二级分类查询出来添加到一级分类中的二级分类集合中
        list.forEach(productList -> {
            QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
            //获取一级分类的id 查询所对应的二级分类的信息
            commodityQueryWrapper.eq("Mobile_end_Product_List_id",productList.getId());
            List<Commodity> commodityList = commodityService.list(commodityQueryWrapper);
            //将查询到的数据添加到一级分类的集合中
            productList.setCommodityList(commodityList);
            //遍历二级分类 将商品详细信息查询出来添加到二级分类中的商品详细信息集合中
            commodityList.forEach(commodity -> {
                QueryWrapper<Details> detailsQueryWrapper = new QueryWrapper<>();
                //获取二级分类的id 查询所对应的商品详细信息
                detailsQueryWrapper.eq("Mobile_end_CommodityId",commodity.getCommodityId());
                if (!StringUtils.isEmpty(queryData.getNamekw()))
                    detailsQueryWrapper.like("info",queryData.getNamekw());
                List<Details> detailsList = detailsService.list(detailsQueryWrapper);
                //将查询到的数据添加到二级分类的集合中
                commodity.setDetailsList(detailsList);
            });
        });
        return R.ok().data("items",list).data("total",page.getTotal())
                .data("current",page.getCurrent()).data("pagesize",page.getSize());
    }


    /**
     * 新增商品分类
     * @param productList
     * @return
     */
    @PostMapping("save")
    public R saveP(@RequestBody ProductList productList){
        return productListService.save(productList)?R.ok():R.error();
    }

    /**
     * 修改商品分类信息
     * @param productList
     * @return
     */
    @PostMapping("updateById")
    public R updateById(@RequestBody ProductList productList){
        return productListService.updateById(productList)?R.ok():R.error();
    }


    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @DeleteMapping("removeById/{id}")
    public R deleteByid(@PathVariable Long id){
        QueryWrapper<Commodity> commodityQueryWrapper = new QueryWrapper<>();
        commodityQueryWrapper.eq("Mobile_end_Product_List_id",id);
        List<Commodity> list = commodityService.list(commodityQueryWrapper);
        //判断下级目录是否还有商品   如果没有 可直接删除
        if (CollectionUtils.isEmpty(list))
            return productListService.removeById(id)?R.ok():R.error();
        else
            return R.error("此目录下还有商品，无法删除!");
    }
}

